<!-- images, css, and js come from this directory only and have a limited name 
     of normal a-z, the dot, and numbers, and some other characters. Simpler is safer.
-->
<html>
  <link   href="support/bootstrap.min.css" rel="stylesheet">
  <script src="support/jquery-2.1.3.min.js"></script>
  <script src="support/bootstrap.min.js"></script>

  <head>
    <script type="text/javascript">

      //
      // This function just redirects the browser to the
      // URL for the GIST
      // 
      function uploadBackTraceToGist_onUpload_redirect( j ) {
          var earl = j["html_url"];
          console.log( "going to: " + earl );
          window.location.replace( earl );
      }

      // 
      // Because GIST is used by many submit buttons it is abstracted
      // pass in a callback which will be executed after the GIST upload
      // so you can embed a link back to the GIST in a different report.
      // 
      function uploadBackTraceToGist( callback ) {
         console.log('ok');
	 var bugdesc = $('#bugdesc').val();

         var report = bugdesc 
	    + "\n--------------------------------------------------------------------------------\n" 
	    + `
 {{report_raw}}
`;
         var data = {
   	   "description": "{{title_raw}}",
	   "public": true,
	   "files": {
	     "file1.txt": {
	        "content": report
	     }
           }
	 };	 

         console.log("ok 1");
	 var xhr = new XMLHttpRequest();
	 xhr.onreadystatechange = function()
	 {
            console.log("onreadystatechange()  r:" + xhr.readyState );
            console.log("onreadystatechange() st:" + xhr.status );
	    if (xhr.readyState==4 && (xhr.status==201 || xhr.status==200))
   	    {
	        console.log(xhr.responseText);
		var j = JSON.parse( xhr.responseText );
		callback( j );
	    }
	 };
	 xhr.onloadend = function () {
	    // done
            console.log('done');
	 };
	 xhr.open( "POST", "https://api.github.com/gists", true);
	 xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');

         console.log(JSON.stringify(data));
	 // send the collected data as JSON
	 xhr.send(JSON.stringify(data));
      }

      //
      // Sent to GIST and redirect there.
      // top level function for the HTML to call
      // 
      function sendgist() {
         uploadBackTraceToGist( uploadBackTraceToGist_onUpload_redirect );
      }

      //
      // When we have a GIST upload, create a new github issue 
      // which links to the backtrace on GIST
      //
      function uploadBackTraceToGist_onUpload_createGithubIssue( j ) {
          var earl = j["html_url"];
          console.log( "gist earl: " + earl );
          var bt = $('#report').val();
  	  var bugdesc = $('#bugdesc').val();
          var report = bugdesc 
	    + "%0A--------------------------------------------------------------------------------%0A" 
	    + "Backtrace available at " + earl;

         // Note that 'bt' has the 4kb clean top of backtrace if we want
//	    + bt;

          console.log( report );
          var earl = "https://github.com/fontforge/fontforge/issues/new?title={{title}}&body=" 
                  + report;
          window.location.replace( earl );
      }

      //
      // Sent the backtrace to GIST and create a new github issue linking
      // to that backtrace...
      // top level function for the HTML to call
      // 
      function sendgithub() {
         uploadBackTraceToGist( uploadBackTraceToGist_onUpload_createGithubIssue );
      }
    </script>
  </head>
  <body>


   <div class="container">

     <div class="masthead">
       <h3 class="text-muted"><img src="fontforge.svg" />&nbsp;Unfortunately FontForge has crashed</h3>
     </div>

     <div class="jumbotron">
      <form class="form-desc">
        <h2 class="form-desc-heading">Please let us know what you were doing...</h2>
        <p>If FontForge keeps crashing when you do a particular series
        of things then letting the developers know that will help them
        to reproduce the problem. It is often the case that being able
        to trigger the issue helps a developer to work out and remove
        what is behind the bug. If you were editing a font file that
        you can include a link to that might help developers trigger
        the problem.</p>
        <label for="bugdesc" class="sr-only"></label>
	<textarea id="bugdesc" class="form-control" rows="5" placeholder="please enter what happened before the crash..." autofocus></textarea>
        <textarea id="report" hidden>{{report}}</textarea>
      </form>
    </div> <!-- jumbotron -->

    <div class="row">
      <div class="col-lg-4">
        <h2>No signing in...</h2>
        <p>Keep it simple, just send the report and be done...</p>
	<p>Report without any login using <a href="javascript:sendgist()">gist</a>.

        <p><a class="btn btn-primary" href="javascript:sendgist()" role="button">Just send without login</a></p>
      </div>
      <div class="col-lg-6">
        <h2>Or Better: Track it with Github...</h2>
        <p>If you are a github user then you can click below to create a new issue. This lets you track the resolution and be notified when the bug is fixed or if a developer might like some more information in order to track this one down.</p>
        <p><a class="btn btn-primary" href="javascript:sendgithub()" role="button">Create Github Issue</a></p>
      </div>
    </div>

  </div>

  </body>
</html>

